package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtResourceDao;
import com.intellagric.mapper.AgtResourceMapper;
import com.intellagric.module.agritech.AgtResourceVo;
import com.intellagric.pojo.AgtResource;
import com.intellagric.pojo.AgtResourceExample;
import com.intellagric.service.agritech.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;


/**
 * @Author: cwq
 * @Date: 2019/2/22 20:43
 * @Description:
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private AgtResourceMapper agtResourceMapper;

    @Autowired
    private AgtResourceDao agtResourceDao;

    @Override
    public ResponseMessage addResource(AgtResource resource) {
        resource.setId(UUIDUtils.getID());
        resource.setCreatedTime(new Date());
        return agtResourceMapper.insertSelective(resource) == 1 ? ResponseMessage.success().add("add", resource) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteResource(String id) {
        return agtResourceMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editResource(AgtResource resource) {
        return agtResourceMapper.updateByPrimaryKeySelective(resource) == 1 ? ResponseMessage.success().add("update", resource) : ResponseMessage.fail();
    }

    @Override
    public AgtResource getResourceById(String id) {
        return agtResourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getResourceList() {
        AgtResourceExample agtResourceExample = new AgtResourceExample();
        agtResourceExample.setOrderByClause("created_time ASC");
        agtResourceExample.createCriteria().andIdIsNotNull();
        List<AgtResource> agtResourceList = agtResourceMapper.selectByExample(agtResourceExample);
        return LayuiDataGridResult.success().add(agtResourceList, agtResourceList.size());
    }

    @Override
    public LayuiDataGridResult getResourceByPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        AgtResourceExample resourceExample = new AgtResourceExample();
        resourceExample.createCriteria().andIdIsNotNull();
        List<AgtResource> resourceList = agtResourceMapper.selectByExample(resourceExample);
        return LayuiDataGridResult.success().add(resourceList, this.getResourceList().getCount());
    }

    @Override
    public LayuiDataGridResult getResourceListByVo(AgtResourceVo agtResourceVo, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AgtResource> resourceList = agtResourceDao.selectResourceListByVo(agtResourceVo);
        return LayuiDataGridResult.success().add(resourceList, resourceList.size());
    }
}
